package main

import "golang.org/x/tour/tree"
import "fmt"

// Walk 步进 tree t 将所有的值从 tree 发送到 channel ch。
func Walk(t *tree.Tree, ch chan int){
	var walker func(t *tree.Tree)
	walker = func(t *tree.Tree){
		if t==nil{
			return
		}
		walker(t.Left)
		ch <- t.Value 
		walker(t.Right)
	}
	walker(t)
	close(ch)
	
}

// Same 检测树 t1 和 t2 是否含有相同的值。
func Same(t1, t2 *tree.Tree) bool{
	c1:=make(chan int)
	c2:=make(chan int)
	go Walk(t1,c1)
	go Walk(t2,c2)
	for i:=0;i<10;i++{
		v1:= <- c1
		v2 := <- c2
		if v1 != v2{
			return false
		}
	}
	return true
	
}

func main() {
	ch := make(chan int)
	go Walk(tree.New(1), ch)
	for v := range(ch){
		fmt.Print(v," ")
	}
	fmt.Println()
	fmt.Println(Same(tree.New(1),tree.New(2)))
	fmt.Println(Same(tree.New(1),tree.New(1)))
}
